<?php

/**
 * This is the model class for table "tbl_product".
 *
 * The followings are the available columns in table 'tbl_product':
 * @property integer $id
 * @property string $name
 * @property string $price
 * @property integer $amount
 * @property string $description
 * @property string $image
 * @property string $date_added
 * @property string $last_update
 * @property integer $category_id
 * @property integer $manufacturer_id
 * @property integer $warehouse_id
 *
 * The followings are the available model relations:
 * @property Cart[] $carts
 * @property Order[] $tblOrders
 * @property Category $category
 * @property Manufacturer $manufacturer
 * @property Warehouse $warehouse
 */
class Product extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Product the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_product';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, image', 'required'),
            array('amount, category_id, manufacturer_id, warehouse_id', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 64),
            array('image', 'length', 'max' => 128),
            array('price', 'length', 'max' => 16),
            array('description, image, last_update', 'safe'),
            array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg,jpeg,gif,png', 'maxSize'=>1024 * 1024 * 3),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, price, amount, description, image, date_added, last_update, category_id, manufacturer_id, warehouse_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'carts' => array(self::HAS_MANY, 'Cart', 'product_id'),
            'tblOrders' => array(self::MANY_MANY, 'Order', 'tbl_order_product(product_id, order_id)'),
            'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
            'manufacturer' => array(self::BELONGS_TO, 'Manufacturer', 'manufacturer_id'),
            'warehouse' => array(self::BELONGS_TO, 'Warehouse', 'warehouse_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Sản phẩm',
            'name' => 'Tên',
            'price' => 'Giá',
            'amount' => 'Số lượng',
            'description' => 'Mô tả',
            'image' => 'Ảnh',
            'date_added' => 'Thêm vào',
            'last_update' => 'Cập nhật',
            'category_id' => 'Thể loại',
            'manufacturer_id' => 'Sản xuất',
            'warehouse_id' => 'Kho hàng',
        );
    }

    /**
     * This is invoked before the record is saved.
     * @return boolean whether the record should be saved.
     */
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->date_added = $this->last_update = date('Y-m-d');                
            }
            else
                $this->last_update = date('Y-m-d');
            return true;
        }
        else
            return false;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('price', $this->price, true);
        $criteria->compare('amount', $this->amount);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('image', $this->image, true);
        $criteria->compare('date_added', $this->date_added, true);
        $criteria->compare('last_update', $this->last_update, true);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('manufacturer_id', $this->manufacturer_id);
        $criteria->compare('warehouse_id', $this->warehouse_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}